#
#   This file contains block and color
#   classes for spites
#

import pygame, random, sys, math
import colorsnew as colorlib
import spites_physics as P
from spites_config import *


class ColorScheme():
    def __init__(self):
        self.black = colorlib.random_color('black')
        self.white = colorlib.random_color('white')
        self.red = colorlib.random_color('red')
        ## random color from groups

        self.new_block_color = random.choice(colorlib.color_groups)

    def newColors(self):
        self.black = colorlib.random_color('black')
        self.white = colorlib.random_color('white')
        self.red = colorlib.random_color('red')
        self.new_block_color = random.choice(colorlib.color_groups)


#----------- Block class. You eat blocks. ------------    
class Block(pygame.sprite.Sprite):
   
    def __init__(self, color, width, height, max_x_pos = WIDTH, max_y_pos = HEIGHT):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width + random.randrange(-width//3, width//3), height + random.randrange(-height//3, height//3)])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.speed = [0, 1]
        self.bad = False
        self.max_x_pos = max_x_pos
        self.max_y_pos = max_y_pos

    def move(self, xdiff, ydiff, xpos, ypos):
        if (self.rect.x > self.max_x_pos):
            self.rect.x = 0
            
        if (self.rect.x < 0):
            self.rect.x = self.max_x_pos

        if (self.rect.y > self.max_y_pos):
            self.rect.y = 0
            
        if random.randrange(100) < PROB_TO_MOVE_AWAY:
            dist = P.distance([xpos, ypos], [self.rect.x, self.rect.y])
            if (dist < MOVE_AWAY_DIST):
                self.speed[0] -= P.block_move_away_acceleration(xpos - self.rect.x)
                self.speed[1] -= P.block_move_away_acceleration(ypos - self.rect.y)
            
            if P.abs_speed(self.speed) > 0.4:
                self.speed[0] = P.block_deceleration(self.speed[0])
                self.speed[1] = P.block_deceleration(self.speed[1])    
        
        if random.randrange(100) < GRAVITY_PROB and self.speed[1] < NORMAL_SPEED:
            self.speed[1] += GRAVITY_CONSTANT
                
        self.rect.x += xdiff + self.speed[0]
        self.rect.y += ydiff + self.speed[1]

# ----------- an enemy block! -------------------------
# this should in some way derive from Block()
# (which it actually does but not enough)
class BadBlock (Block):
    """ A very VERY bad block indeed."""
    def __init__(self, color, width, height):
        Block.__init__(self, color, width, height)
        self.bad = True
        self.speed = [0, 0]
        self.dist = [random.randrange(-2, 3), random.randrange(-2, 3)]
        
        # xpos, ypos are the player position
    def move(self, xpos, ypos):
        # they tend to clut together after a while
        self.rect.x += (self.speed[0] + self.dist[0])
        self.rect.y += (self.speed[1] + self.dist[1])
        
        self.speed[0] += P.bad_block_acceleration(xpos - self.rect.x)
        self.speed[1] += P.bad_block_acceleration(ypos -self.rect.y)
        
        ## friction
        self.speed[0] = P.block_deceleration(self.speed[0])
        self.speed[1] = P.block_deceleration(self.speed[1])
        
pygame.mixer.init()
snd = pygame.mixer.Sound
intro = snd('Theme2.ogg')
theme = snd('Theme.ogg')
point = snd('point.wav')
game_over = snd('game_over.ogg')

